//表单
layui.use(['table'],function () {
    var table = layui.table;
    table.render({
        elem:'#carousel'
        ,size:'lg'
        ,url:'/admin/rotation/get_page'
        ,cols:[[
            {field:'articleVO.title',title:'文章',width:'300'
                ,templet: function (data) {
                    return data.articleVO.title
                }}
            ,{field:'coverImage',title:'封面图片',width:'300'}
            ,{field:'createTime',title:'创建时间',width:'200'}
            ,{field:'sort',title:'排序依据',width:'100'}
            ,{title: "操作",fixed:'right',width:'393',align:'center',toolbar:'#bar'}
        ]]
        ,page:true
        ,id:'carousel'
    }),table.on('tool(carousel)', function(obj) { //注：tool 是工具条事件名，test 是 table 原始容器的属性 lay-filter="对应的值"
        var data = obj.data; //获得当前行数据
        var layEvent = obj.event; //获得 lay-event 对应的值（也可以是表头的 event 参数对应的值）
        var tr = obj.tr; //获得当前行 tr 的 DOM 对象（如果有的话）
        var id = data.id;
        if (layEvent == "select"){
            $.ajax({
                url:'/admin/rotation/get/' + id
                ,method:'get'
                ,success:(response) =>{
                    console.log(id)
                    console.log(response)
                    $("#sel_title").val(response.articleVO.title)
                    $("#sel_coverImage").val(response.coverImage)
                    $("#sel_ctime").val(response.createTime)
                    var open = layer.open({
                        type: 1
                        ,area: ['430px', '400px']
                        ,title: "详情"
                        ,content: $("#sel_carousel")
                        ,shade: 0
                        ,btn: []
                        ,cancel: function (layero, index) {//关闭按钮
                            layer.close(open);
                        }
                    })
                }
            })
        }else if(layEvent == "update"){
            $.ajax({
                url:'/admin/rotation/get/' + id
                ,method:'get'
                ,success:(response) =>{
                    $("#upd_title").val(response.articleVO.title)
                    $("#upd_file").val(response.coverImage)
                    $("#upd_sort").val(response.sort)
                    var open = layer.open({
                        type:1
                        ,area: ['480px', '500px']
                        ,title: "详情"
                        ,content: $("#u_carousel")
                        ,shade: 0
                        ,btn: ['修改','取消']
                        ,btn1:function (layuio,index) {
                            var id = response.id
                            var articleId = response.articleVO.id
                            var coverImage = $("#upd_file").val()
                            var sort = $("#upd_sort").val()
                            var formData = {
                                id:id
                                ,articleId:articleId
                                ,coverImage:coverImage
                                ,sort:sort
                            }
                            $.ajax({
                                url:'/admin/rotation/update'
                                ,method:'put'
                                ,data:formData
                                ,success:(response) =>{
                                    if (response.code == 0) {
                                        alert("修改轮播图成功")
                                        location.reload()
                                    } else {
                                        alert(response.msg)
                                        return false
                                    }
                                }
                            })
                        }
                        ,cancel: function (layero, index) {//关闭按钮
                            layer.close(open);
                        }
                    })
                }
            })

        }else if(layEvent == "delete"){
            layer.confirm('你确定要删除吗？',{
                time:0,
                title:'删除角色',
                btn:['确定','取消'],
            },function (index) {
                $.ajax({
                    url:"/admin/rotation/delete"
                    ,method:'delete'
                    ,data:{id:id}
                    ,success: (response)=>{
                        if(response.code == 0){
                            alert("删除成功")
                            location.reload();
                        }else{
                            alert(response.msg)
                        }
                    }
                })
            })
        }
    })

})

//修改-列表
$("#choice").click(function () {
    layer.closeAll()
    var upd_tags = layer.open({
        type:1,
        area:['1300px','600px'],
        title: "修改轮播图"
        ,content: $("#upd_c")
        ,shade: 0
        ,offset: 'r'
        ,btn: []
        ,cancel: function(layero,index){//关闭按钮
            layer.close(upd_tags);
        }
    });
})
layui.use(['table'],function () {
    var table = layui.table;
    table.render({
        elem:'#upd_carousel'
        ,size:'lg'
        ,url:'/admin/rotation/super_rotation_get_article_page'
        ,id:'upd_select'
        ,cols:[[
            {field:'title',title:'文章',width:'300'}
            ,{field:'coverImage',title:'封面图片',width:'300'}
            ,{field:'description',title:'简介',width:'200'}
            ,{field:'categoryName',title:'分类',width:'170'}
            // ,{field:'sort',title:'排序依据',width: '150'}
            ,{title: "操作",fixed:'right',width:'280',align:'center',toolbar:'#bar2'}
        ]]
        ,page:true
    });
    $("#upd_select").click(function () {
        var title = $("#updtitle").val()
        var categoryId = $("#updcategory").val()
        var description = $("#upddescription").val()
        var formData = {
            title:title
            ,categoryId:categoryId
            ,description:description
            ,page:1
            ,limit:10
        }
        $.ajax({
            url:"/admin/rotation/super_rotation_get_article_page"
            ,method:"get"
            ,data:formData
            ,success: (response) =>{
                table.reload('upd_select',{
                    where:{
                        title:title
                        ,categoryId:categoryId
                        ,description:description
                        ,page:1
                        ,limit:10
                    }
                    ,page:{
                        curr:1
                    }
                })
            }
        })
    })
    table.on('tool(upd_carousel)', function(obj) { //注：tool 是工具条事件名，test 是 table 原始容器的属性 lay-filter="对应的值"
        var data = obj.data; //获得当前行数据
        var layEvent = obj.event; //获得 lay-event 对应的值（也可以是表头的 event 参数对应的值）
        var tr = obj.tr; //获得当前行 tr 的 DOM 对象（如果有的话）
        var id = data.id;
        var articledata = layui.table.cache["carousel"]
        var datas = new Array()
        console.log(articledata)
        for (i = 0; i<articledata.length;i++){
            var  articleid = articledata[i]

        }
        // console.log(articleid.articleVO.id) //从轮播图查文章id
        // console.log(articleid.id) //轮播图列表id
        // console.log(id)//当前行文章id
        // console.log(data.title)
        if (layEvent == "do"){
            articleid.articleVO.id = id
            $.ajax({
                url:'/admin/rotation/get/' + articleid.id
                ,method:'get'
                ,success:(response) =>{
                    console.log(response)
                    $("#upd_title").val(data.title)
                    $("#upd_file").val(response.coverImage)
                    $("#upd_sort").val(response.sort)

                    var open = layer.open({
                        type:1
                        ,area: ['480px', '500px']
                        ,title: "修改轮播图"
                        ,content: $("#u_carousel")
                        ,shade: 0
                        ,btn: ['修改','取消']
                        ,btn1:function (layuio,index) {
                            var cid = articleid.id
                            var articleId = id
                            var coverImage = $("#upd_file").val()
                            var sort = $("#upd_sort").val()
                            var formData = {
                                id:cid
                                ,articleId:articleId
                                ,coverImage:coverImage
                                ,sort:sort
                            }
                            $.ajax({
                                url:'/admin/rotation/update'
                                ,method:'put'
                                ,data:formData
                                ,success:(response) =>{
                                    if (response.code == 0) {
                                        alert("修改轮播图成功")
                                        location.reload()
                                    } else {
                                        alert(response.msg)
                                    }
                                }
                            })
                        }
                        ,cancel: function (layero, index) {//关闭按钮
                            layer.close(open);
                        }
                    })
                }
            })


        }

    })
})


//添加轮播图
$(function () {
    $("#add").click(function () {
        var add_tags = layer.open({
            type:1,
            area:['1300px','600px'],
            title: "新增轮播图"
            ,offset: 'r'
            ,content: $("#add_c")
            ,shade: 0
            ,btn: []
            ,cancel: function(layero,index){//关闭按钮
                layer.close(add_tags);
            }
        });

    })
})
layui.use(['table'],function () {
    var table = layui.table;
    table.render({
        elem:'#add_carousel'
        ,size:'lg'
        ,url:'/admin/rotation/super_rotation_get_article_page'
        ,id:'select'
        ,cols:[[
            {field:'title',title:'文章',width:'300'}
            ,{field:'coverImage',title:'封面图片',width:'300'}
            ,{field:'description',title:'简介',width:'300'}
            ,{field:'categoryName',title:'分类',width:'170'}
            // ,{field:'sort',title:'排序依据',width: '150'}
            ,{title: "操作",fixed:'right',width:'200',align:'center',toolbar:'#bar2'}
        ]]
        ,page:true
    });
    //模糊查询
    $("#add_select").click(function () {
        var title = $("#addtitle").val()
        var categoryId = $("#addcategory").val()
        var description = $("#adddescription").val()
        console.log(title)
        console.log(categoryId)
        var formData = {
            title:title
            ,categoryId:categoryId
            ,description:description
            ,page:1
            ,limit: 10
        }
        $.ajax({
            url:"/admin/rotation/super_rotation_get_article_page"
            ,method:"get"
            ,data:formData
            ,success: (response) => {
                table.reload('select',{
                    where:{
                        title:title
                        ,categoryId:categoryId
                        ,description:description
                        ,page:1
                        ,limit: 10
                    }
                    ,page:{
                        curr:1
                    }
                })
            }
        })
    })
    table.on('tool(add_carousel)', function(obj) { //注：tool 是工具条事件名，test 是 table 原始容器的属性 lay-filter="对应的值"
        var data = obj.data; //获得当前行数据
        var layEvent = obj.event; //获得 lay-event 对应的值（也可以是表头的 event 参数对应的值）
        var tr = obj.tr; //获得当前行 tr 的 DOM 对象（如果有的话）
        var id = data.id;
        // var articledata = layui.table.cache["add_carousel"]
        // var datas = new Array()
        // console.log(articledata)
        $("#do_title").val(data.title)
        var do_open = layer.open({
            type:1
            ,area: ['480px','400px']
            ,title:"新增轮播图"
            ,content:$("#do_carousel")
            ,shade:0
            ,btn:['确定','取消']
            ,btn1:function (index,layuio) {
                // for (i = 0;i<articledata.length; i++){
                //     datas.push(articledata[i].id)
                // }
                var image = $("#do_file").val()
                var sort = $("#do_sort").val()
                var id = data.id
                // console.log(datas)
                console.log(image)
                console.log(sort)
                console.log(id)
                var formData={
                    articleId:id
                    ,coverImage:image
                    ,sort:sort
                }
                $.ajax({
                    url: '/admin/rotation/add'
                    ,method: "post"
                    ,data:formData
                    ,success: (response)=>{
                        if (response.code == 0){
                            alert("新增轮播图成功")
                            location.reload()
                        }else{
                            alert(response.msg)
                        }
                    }
                })
            }
            ,cancel: function (layuio,index) {
                layer.close(do_open)

            }
        })
    })
})

//增加
//点击图片上传上传图片地址功能
$("#add_image").click(function () {
    var formData = new FormData();
    formData.append('file', $('#addfile')[0].files[0]);
    $.ajax({
        url: '/admin/article/upload_image',
        method: "post",
        data: formData,
        processData: false,
        contentType: false, // 不要设置Content-Type请求头
        success: (response) => {
            if (response.code == 0) {
                $("#pre").attr('src', response.data)
                $(".file_text").val(response.data)
                $()
            } else {
                alert(response.msg)
            }
        }
    })
    return false
})


//修改
//点击图片上传上传图片地址功能
$("#upd_image").click(function () {
    var formData = new FormData();
    formData.append('file', $('#updfile')[0].files[0]);
    $.ajax({
        url: '/admin/article/upload_image',
        method: "post",
        data: formData,
        processData: false,
        contentType: false, // 不要设置Content-Type请求头
        success: (response) => {
            if (response.code == 0) {
                $("#pre").attr('src', response.data)
                $(".file_text").val(response.data)
                $()
            } else {
                alert(response.msg)
            }
        }
    })
    return false
})
